<?
require_once('config.php');
$action = getValue('action','str','POST');
if($action == 'submit'){
   $cat_name = getValue('cat_name','str','POST');
   $cat_id   = getValue('cat_id','int','POST');
   $vid_type = getValue('vid_type','int','POST',0);
   $channel_name = getValue('channel_name','str','POST','');
   $start_index  = getValue('start_index','int','POST',0);
   $max_results  = getValue('max_results','int','POST',0);
   $received     = getValue('has_get','int','POST',0);
   $link_api = "http://gdata.youtube.com/feeds/api/users/".$channel_name."/uploads?v=2&alt=jsonc&max-results=".$max_results."&start-index=".$start_index;
   if(get_http_response_code($link_api) != 200){
      //echo 'Channel hoặc link video không tồn tại. Vui lòng kiểm tra lại !';
      echo 'Link:' . $link_api . '<br />';
      exit('Error:' . get_http_response_code($link_api));
   }
   $respone = json_decode(file_get_contents($link_api),true);
   if(count($respone) >0 && isset($respone['data']['items'])){
      $totalItems = $respone['data']['totalItems'];
      $array_videos = $respone['data']['items'];
   }else{
      die('Không tồn tại Channel hoặc đã hết video') ;
   }
   if(count($array_videos) > 0){
      $value_insert = '';
      $sql_insert   = '';
      foreach($array_videos as $value){
         $vid_id_youtube = $value['id'];
         $vid_name  = handelVidName($value['title']); // Xử lý tên video
         $vid_lyric = '';
         $vid_duration = formatDuraction($value['duration']);
         $vid_image    = handleNameImage($value['thumbnail']['hqDefault']);
         $value_insert .= "('" . $vid_name . "',
                            " . $cat_id . ",
                            '" . $vid_duration . "',
                            '" . $vid_image . "',
                            " . time() . ",
                            '" . $vid_id_youtube . "',
                            " . $vid_type . "),";
         
      }
      if($value_insert != ''){
         $value_insert = substr($value_insert,0,-1); 
         $sql_insert = "INSERT INTO 
                        video(vid_name,
                              cat_id,
                              vid_duration,
                              vid_image,
                              vid_date,
                              vid_id_youtube,
                              vid_type)
                        VALUES
                           " . $value_insert;  
      }
      if($sql_insert != ''){
         mysql_query($sql_insert);
         $rows_inserted = mysql_affected_rows();
         $received += $rows_inserted;
      }
      
      

   }
   echo '<span style="color: #01B42C;font-size: 14px;font-weight: bold;">Thêm thành công '.$rows_inserted.' videos </span>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Thêm mới <?=$moduol_name?></title>
   <?=$css_global?>
   <?=$js_global?>
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
    $(document).on('click','.reload_tab' ,function(){
      window.location.reload();
    });
  });
  </script>
</head>
<div class="row-fluid">
   <div class="head">
      <div class="isw-grid"></div>
      <h1>Thêm mới <?=$moduol_name?></h1>      
      <div class="reload_tab" title="Reload"></div>                         
      <div class="clear"></div>
   </div>
   <div class="block-fluid">
      <form class="form-horizontal" name="add" action="" method="POST" enctype="multipart/form-data"  onsubmit="">
         <input type="hidden" name="action" value="submit" />
         <fieldset>
            <div class="control-group">
               <label class="control-label" for="input01">Tên channel</label>
               <div class="controls">
						<input type="text" value="<?=isset($channel_name) ? $channel_name : ''?>" class="input-xlarge tooltip-right span5" id="channel_name" name="channel_name" title="Tên channel"/>
					</div>
            </div>
            
            <div class="control-group">
               <label class="control-label" for="input01">Loại nhạc</label>
               <div class="controls">
                  <select id="vid_type" name="vid_type">
                     <option value="0">Không lời</option>
                     <option value="1">Có lời</option>
                  </select>
               </div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Từ video</label>
               <div class="controls">
						<input type="text" value="<?=isset($received) ? $received  + 2 : 0?>" class="input-xlarge tooltip-right span3" id="start_index" name="start_index" title="Vị trí video bắt đầu lấy"/>
					</div>
               
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Tổng số</label>
               <div class="controls">
						<input type="text" class="input-xlarge tooltip-right span3" id="max_results" name="max_results" title="Tổng số video muốn lấy"/>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Đã lấy từ channel</label>
               <div class="controls">
						<input type="text" value="<?=isset($received) ? $received : 0?>" class="input-xlarge tooltip-right span3" id="has_get" name="has_get" readonly="true" title="Video đã lấy từ channel"/>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Chọn thể loại</label>
               <div class="controls">
						<select id="cat_id" name="cat_id">
                     <option value="0">Không chọn</option>
                     <?if($count_cat > 0):?>
                        <?foreach($array_all_cat as $key=>$value):?>
                           <option value="<?=$key?>"><?=$value?></option>
                        <?endforeach?>
                     <?endif?>
                  </select>
					</div>
            </div>
            <div class="form-actions">
					<button type="submit" class="btn btn-info">Thêm</button>
					<button type="reset"  class="btn btn-warning">Làm lại</button>
				</div>
         </fieldset>
      </form>
   </div> 
</div>